package cn.jx.asc.service.impl;

import cn.jx.asc.dao.mapper.PipeMapper;
import cn.jx.asc.dao.mapper.PipeStatusMapper;
import cn.jx.asc.pojo.bo.PipeBO;
import cn.jx.asc.pojo.bo.PipeInfoBO;
import cn.jx.asc.pojo.entity.Pipe;
import cn.jx.asc.pojo.entity.PipeStatus;
import cn.jx.asc.pojo.vo.PipeVO;
import cn.jx.asc.service.PipeService;
import cn.jx.asc.service.PipeStatusService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lhg
 * @since 2021-06-28
 */
@Service
public class PipeServiceImpl extends ServiceImpl<PipeMapper, Pipe> implements PipeService {

    @Autowired
    private PipeMapper pipeMapper;
    @Autowired
    private PipeStatusMapper statusMapper;

    @Transactional(propagation = Propagation.SUPPORTS,rollbackFor=Exception.class)
    @Override
    public PipeBO queryAllPipeData(long current, long size) {


        final PipeBO pipeBO = new PipeBO();

        final Page<Pipe> page = new Page<>(current, size);

        final QueryWrapper<Pipe> wrapper = new QueryWrapper<>();

        return getPipeBO(pipeBO, page, wrapper);
    }

    @Transactional(propagation = Propagation.SUPPORTS,rollbackFor=Exception.class)
    @Override
    public PipeBO queryPipeDataByTime(Long current, Long size, Date startTime, Date endTime) {

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        final String sTime = dateFormat.format(startTime);

        final String eTime = dateFormat.format(endTime);

        final PipeBO pipeBO = new PipeBO();

        final Page<Pipe> page = new Page<>(current, size);

        final QueryWrapper<Pipe> wrapper = new QueryWrapper<>();

        wrapper.between("created_time",sTime,eTime);

        return getPipeBO(pipeBO, page, wrapper);
    }

    private PipeBO getPipeBO(PipeBO pipeBO, Page<Pipe> page, QueryWrapper<Pipe> wrapper) {
        wrapper.orderByDesc("created_time");

        final Page<Pipe> pipePage = pipeMapper.selectPage(page, wrapper);
        final List<Pipe> records = pipePage.getRecords();
        final List<PipeVO> voList = pToCopy(records);

        pipeBO.setCurrent(pipePage.getCurrent());
        pipeBO.setSize(pipePage.getSize());
        pipeBO.setTotal(pipePage.getTotal());
        pipeBO.setList(voList);
        return pipeBO;
    }

    @Override
    public List<PipeVO> queryPipeDataByTime(Date startTime, Date endTime) {
        List<PipeVO> vos = null;

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        final String sTime = dateFormat.format(startTime);

        final String eTime = dateFormat.format(endTime);

        final QueryWrapper<Pipe> wrapper = new QueryWrapper<>();

        wrapper.orderByDesc("created_time");

        wrapper.between("created_time",sTime,eTime);
        final List<Pipe> pipes = pipeMapper.selectList(wrapper);
        vos = pToCopy(pipes);
        return vos;
    }

    @Override
    public PipeInfoBO queryPipeInfoById(Integer pipeId) {
        Pipe pipe =null;
        PipeInfoBO infoBO = new PipeInfoBO();
        final QueryWrapper<Pipe> wrapper = new QueryWrapper<>();
        wrapper.eq("pipe_id",pipeId);
        wrapper.orderByDesc("created_time");
        final List<Pipe> pipes = pipeMapper.selectList(wrapper);
        if (!pipes.isEmpty()){
            pipe = pipes.get(0);
        }
        if (pipe!=null){
            infoBO.setPipeId(pipe.getPipeId());
            infoBO.setInjectionVolume(pipe.getInjectionVolume());
            infoBO.setLiquidLevel(pipe.getLiquidLevel());
            infoBO.setValveStatus(pipe.getValveStatus());
            final PipeStatus pipeStatus = statusMapper.selectById(pipeId);
            if (pipeStatus!=null){
                infoBO.setPipeHeight(pipeStatus.getHeight());
                infoBO.setConsoleStatus(pipeStatus.getConsoleStatus());
            }
        }
        return infoBO;
    }


    private static List<PipeVO> pToCopy(List<Pipe> records){

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        List<PipeVO> voList = new ArrayList<>();

        final DecimalFormat df = new DecimalFormat("0.0");

        for (Pipe p :
                records) {
            PipeVO pipeVO = new PipeVO();
            pipeVO.setPipeId(p.getPipeId());
            if (p.getAngle() != null) {
                pipeVO.setAngle(df.format(p.getAngle() * 0.1));
            }
            if (p.getMountainId() != null) {
                pipeVO.setMountainId(p.getMountainId());
            }
            if (p.getInjectionVolume() != null) {
                pipeVO.setInjectionVolume(df.format(p.getInjectionVolume() * 0.1));
            }
            if (p.getLiquidLevel()!=null) {
                pipeVO.setLiquidLevel(df.format(p.getLiquidLevel() * 0.1));
            }

            if (p.getCreatedTime()!=null) {
                pipeVO.setCreatedTime(dateFormat.format(p.getCreatedTime()));
            }

            if (p.getValveStatus()!=null) {
                pipeVO.setValveStatus(p.getValveStatus());
            }

            voList.add(pipeVO);
        }
        return voList;
    }
}
